<template>
  <div class="login-container">
    <div class="login-box">
      <h2>妈妈助手登录</h2>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          id="password" 
          v-model="password" 
          placeholder="请输入密码"
          @keyup.enter="login"
        >
      </div>
      <div class="error-message" v-if="errorMsg">{{ errorMsg }}</div>
      <button @click="login" :disabled="isLoading">{{ isLoading ? '登录中...' : '登录' }}</button>
    </div>
  </div>
</template>

<script setup>
import { ref, defineEmits } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const password = ref('')
const errorMsg = ref('')
const isLoading = ref(false)

// 定义事件
const emit = defineEmits(['login-success'])

// 固定密码为
const FIXED_PASSWORD = 'kunkun20250108'

const login = () => {
  if (!password.value) {
    errorMsg.value = '请输入密码'
    return
  }
  
  isLoading.value = true
  
  // 模拟登录请求
  setTimeout(() => {
    if (password.value === FIXED_PASSWORD) {
      // 登录成功，存储登录状态
      localStorage.setItem('isLoggedIn', 'true')
      // 发送登录成功事件
      emit('login-success')
      // 跳转到首页
      router.push('/')
    } else {
      // 登录失败
      errorMsg.value = '密码错误，请重试'
      password.value = ''
    }
    isLoading.value = false
  }, 500)
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 350px;
  padding: 30px 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #2c3e50;
  font-size: 36px; /* 增加标题字体大小 */
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  color: #2c3e50;
  font-size: 18px; /* 调整标签字体大小到18px */
}

input {
  width: 100%;
  padding: 15px 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 18px; /* 调整输入框字体大小到18px */
  box-sizing: border-box;
}

button {
  width: 100%;
  padding: 15px 20px; /* 调整按钮高度 */
  background-color: #42b883;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 20px; /* 调整按钮字体大小到20px */
  font-weight: bold; /* 加粗按钮文字 */
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 15px;
}

button:hover {
  background-color: #3aa876;
}

button:disabled {
  background-color: #a8d5c3;
  cursor: not-allowed;
}

.error-message {
  color: #e74c3c;
  margin-bottom: 15px;
  font-size: 18px; /* 调整错误信息字体大小到18px */
  text-align: center;
}
</style>